﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LeastSquare.model
{
    internal class Function
    {
        public static double Method(List<int> xlist, List<double> ylist, int x)
        {
            //得到年份x的平均值
            double averageX = xlist.Average();
            //得到GDPy的平均值
            double averageY = ylist.Average();
            double numerator = 0;
            double denoinator = 0;
            double a = 0;
            double b = 0;
            //计算a和b
            for (int i = 0; i < xlist.Count; i++)

            {
                numerator += (xlist[i] - averageX) * (ylist[i] - averageY);
                denoinator += (xlist[i] - averageX) * (xlist[i] - averageX);
            }
            b = numerator / denoinator;
            a = averageY - b * averageX;
            //拟合直线
            double y = a + b * x;
            return y;






        }
    }
}
